package Server.Tasks;

import java.util.HashMap;

import Server.Call;
import Server.PhoneServerThread;
import Server.PhoneServer;

public class CallTask extends ServerTask {

	private PhoneServer server;
	
	public CallTask(PhoneServerThread p, int priority, String d, PhoneServer server)
	{
		super(p, priority, d);
		this.server = server;
	}

	public void ProcessTask() {
		PhoneServerThread caller = phone;
		PhoneServerThread callee = server.getExtension(Integer.parseInt(data));
		
		if(callee != null && (callee.getPhoneStatus() == PhoneServerThread.Status.ACTIVATED || callee.getPhoneStatus() == PhoneServerThread.Status.CANNOT_CALL) && server.checkCallLoad()
				&& callee.getPhoneState() == PhoneServerThread.States.IDLE && caller.getCurrentCall() == null && callee.getCurrentCall() == null) {
			Call call = new Call(caller, callee);

			server.addCallToCallList(call);
			caller.setCurrentCall(call);
			callee.setCurrentCall(call);
			caller.sendMessage(new CallerRingingTask(2, null, caller));
			callee.sendMessage(new CalleeRingingTask(2, null, callee));
		} else {
			caller.sendMessage(new CallFailedTask(2, null, caller));
		}		
	}
}
